package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Product;
import cn.wolfcode.crm.domain.ProductStock;
import cn.wolfcode.crm.domain.TotalProductInfo;
import cn.wolfcode.crm.mapper.ProductMapper;
import cn.wolfcode.crm.mapper.ProductStockMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IProductService;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements IProductService{
    @Autowired
    private ProductMapper mapper;
    
    @Autowired
    private ProductStockMapper psMapper;
    
    public void save(Product entity) {
        mapper.insert(entity);
    }

    public void update(Product entity) {
        mapper.updateByPrimaryKey(entity);
    }

    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    public List<Product> list() {
        return mapper.selectAll();
    }

    public Product get(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

	public PageResult query(QueryObject qo) {

		return PageUtils.query(qo, mapper);
	}


    public TotalProductInfo getInfo() {
        TotalProductInfo info = new TotalProductInfo();
        info.setAvgPrice(mapper.getAvgPrice());
        info.setTotalProduct(mapper.getTotalProduct());
        info.setProduct(mapper.getProduct());
        info.setLessProduct(mapper.getLessProduct());
        return info;
    }

	public List<Map<String, String>> queryBrandInfo() {
		return mapper.queryBrandInfo();
	}

    public List<Product> queryProduct() {
        return mapper.queryProduct();
    }

	public List<HashMap<String, Object>> getRepertory(QueryObject qo) {
		List<HashMap<String, Object>> repertoryInfo = new ArrayList<>();
		
		List<ProductStock> products = psMapper.queryForList(qo);
		
		for (int i = 0; i < products.size(); i++) {
			BigDecimal storeNumber = products.get(i).getStoreNumber();
			if(storeNumber.compareTo(new BigDecimal("10")) == -1 ) {
				HashMap<String, Object> map = new HashMap<>();
				map.put("product",products.get(i).getProduct().getName());
				map.put("stockName",products.get(i).getDepot().getName());
				map.put("stocknumber",storeNumber);
				repertoryInfo.add(map);
			}
		}
		
		return repertoryInfo;
	}
    
}
